package main

import (
	"net"
	"strconv"

	"gitee.com/konyshe/gogo"
)

func recvUDPMsg(conn *net.UDPConn, maxMTU int) {
	for {
		buf := make([]byte, maxMTU)

		n, pRemoteAddr, err := conn.ReadFromUDP(buf[0:])
		gogo.CheckError(err)

		if pProPayloadTask != nil {
			pProPayloadTask.Push(&ProPayloadData{buf, n, pRemoteAddr})
		}
	}
}

func LoadFromUDP(recvPort, maxMTU int) *net.UDPConn {
	udp_addr, err := net.ResolveUDPAddr("udp", ":"+strconv.Itoa(recvPort))
	gogo.CheckError(err)

	conn, err := net.ListenUDP("udp", udp_addr)
	gogo.CheckError(err)

	go recvUDPMsg(conn, maxMTU)

	return conn
}
